package com.zhoug.speech.xunfei;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import androidx.annotation.Keep;

/**
 * 讯飞结果解析
 *
 * @Author 35574
 * @Date 2020/7/8
 * @Description
 */

class ResultParser {
    private static final String TAG = ">>>ResultParser";

    static String parseResult(String result) {
        Log.d(TAG, "onResult:result=" + result);
        StringBuilder stringBuffer = new StringBuilder();
        try {
            JSONObject jsonObject = new JSONObject(result);
            //一句话中的单词集合
            JSONArray ws = jsonObject.optJSONArray("ws");
            if (ws != null && ws.length() > 0) {
                for (int i = 0; i < ws.length(); i++) {
                    //第i个单词的识别结果
                    JSONObject items = ws.optJSONObject(i);
                    if (items != null) {
                        //第i个单词的识别不同结果的集合
                        JSONArray cw = items.optJSONArray("cw");
                        if (cw != null && cw.length() > 0) {
                            //默认去第一个识别结果
                            JSONObject item = cw.optJSONObject(0);
                            if (item != null) {
                                String w = item.optString("w");
                                if (w != null) {
                                    stringBuffer.append(w);
                                }
                            }
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
}
